package com.yuanfeng.action.front.order.web;
import com.opensymphony.xwork2.ActionSupport;
import com.yuanfeng.action.front.order.service.OrderService;
import com.yuanfeng.action.front.order.service.OrderServiceImpl;
import com.yuanfeng.pojo.Books;
import com.yuanfeng.pojo.OrderLine;
import com.yuanfeng.pojo.Orders;
import com.yuanfeng.pojo.Users;
import com.yuanfeng.util.WebUtil;
import org.apache.struts2.ServletActionContext;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

/**
 * 订单模块的action
 * @author yuanfeng
 */
public class OrderAction extends ActionSupport  {

    private OrderService orderServiceImpl;

    public void setOrderServiceImpl(OrderServiceImpl orderServiceImpl) {
        this.orderServiceImpl = orderServiceImpl;
    }
    /**
     * 准备订单
     * @return
     */
    public String toOrder() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Orders order = WebUtil.copyRequestToBean(request, Orders.class);
        order.setId(WebUtil.getOrderNo(order.hashCode()));
        order.setOrderdate(WebUtil.getDateStr());
        request.setAttribute("order", order);
        return "toOrder";
    }
    /**
     * 生成订单
     * @return
     */
    public String save() {

        HttpServletRequest request = ServletActionContext.getRequest();
        Orders order = WebUtil.copyRequestToBean(request, Orders.class);
        System.out.println(order);
        Users user = (Users) request.getSession().getAttribute("user");
        order.setUserid(user.getId());
        order.setOrderdate(WebUtil.getDateStr());
        //从map中取出所有的book
        Map<String, Books> car = (Map<String, Books>) request.getSession().getAttribute("car");
        Iterator<Map.Entry<String, Books>> it = car.entrySet().iterator();
        while(it.hasNext()){

                Books book = it.next().getValue();
                OrderLine ol = new OrderLine();
                ol.setId(UUID.randomUUID().toString().replace("-", ""));
                ol.setBookid(book.getId());
                ol.setOrderid(order.getId());
                ol.setAmt(book.getAmt());
                ol.setPrice(book.getCurrentPrice());
                order.getOrderLines().add(ol);
            }
            //上面的数据封装完成，开始保存此订单
            orderServiceImpl.saveOrder(order);
            //从session中清空购物车的信息
            request.getSession().removeAttribute("car");
            request.setAttribute("order", order);

            //这里判断用户的购买方式是货到付款还是在线支付,1货到付款，2在线支付
              if("1".equals(order.getPaytype())){
                  return "arrivepay";
              }else{
                  return "onlinepay";
              }
       }
    /**
     * 查询所有的的订单
     * @return
     */
    public String queryOrders() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Users user = (Users) request.getSession().getAttribute("user");
        List<Orders> orders = orderServiceImpl.queryOrders(user.getId());
        request.setAttribute("orders", orders);
        return "queryOrders";
    }

    /**
     * 根据id查询订单
     * @return
     */
    public String queryOne() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Orders order = WebUtil.copyRequestToBean(request, Orders.class);
        order = orderServiceImpl.oneOrder(order);
        request.setAttribute("order", order);
        return "queryOne";
    }

    /**
     * 取消订单
     * @return
     */
    public String cancleOrder() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Orders order = WebUtil.copyRequestToBean(request, Orders.class);
        orderServiceImpl.cancle(order);
        Users user = (Users) request.getSession().getAttribute("user");
        List<Orders> orders = orderServiceImpl.queryOrders(user.getId());
        request.setAttribute("orders", orders);
        return "cancleOrder";
    }
}
